15/11/2017
Accessing JSON data in elisp
:PUBDATE:
:ID: 339a61c5-ffab-4b55-a334-35ffddc39bde
(require 'json) (let* ((json-object-type 'hash-table) (json-array-type 'list) (json-key-type 'string) (json (json-read-file "test.json"))) (car (gethash "projects" json)))
The let* is required because otherwise the call to json-read-file will not see the previously bound values as let sets them in parallel…
As for the access functions, gethash retrieves a value by key from a hash table, whereas car returns the first list element. If you need a different one, you can use nth with an index.
https://emacs.stackexchange.com/questions/27407/accessing-json-data-in-elisp
Introducing json.el zero
JSON is a lightweight data interchange format based on a subset of JavaScript. You can read all about JSON at json.org. json.el is a JSON parser and generator for Emacs Lisp, which can produce an Emacs Lisp data structure from a JSON object and vice-versa. It’s been Included with Emacs since February 2008.
Using it is pretty straightforward; here are some examples.
TODO org-json.el — conversion between org and json zero
https://www.emacswiki.org/emacs/org-json.el
;; Usage: ;; 1. call (org-json-encode) to convert org(in current buffer) to json, ;; return convert json. ;; 2. call (org-json-decode json) to convert json to org, ;; return converted text. ;; Example: ;; * i1 [2,3,4] ;; * i2 3.4 ;; * i3 "fdsafs" ;; => (("i1" . [2,3,4]) ("i2" . 3.4) ("i3" . "fdsafs")) ;; ;; * i1 ;; [2,3,4] ;; * i2 ;; 3.4 ;; => (("i1" . [2,3,4]) ("i2" 3.4)). ;; * lv1_1 ;; ** lv2a 1 ;; ** lv2b 2 ;; * lv1_2 "tt" ;; => (("lv1_1" . (("lv2a" . 1) ("lv2b" . 2))) ("lv1_2" . "tt")) ;; * vect ;; ** 0 "idx0" ;; ** 1 1.23 ;; ** 2 ;; [5.2, "2.3t", 1] ;; => (("vect" . ["idx0" 1.23 [5.2 "2.3t" 1]])) ;; * long_src ;; +begin_src ;; line1 ;; line2 ;; +end_src ;; => (("long_src" . "line1\nline2\n"))